<?php

class ProductService extends BaseService {

	public function getProducts() {
		$categoryManager = new CategoryManager();

		$categoryManager->query();

		$categories = $categoryManager->getResult();

		foreach ($categories AS $category) {
			$category_id = $category->getCategoryId();

			$productsManager = new ProductsManager();
				
			$filter['product_category_id'] = $category->getCategoryId();
			$filter['current_count'] = array('>', 0);
			$filter['archive'] = 'n';
				
			$productsManager->setFilter($filter);

			$productsManager->query();

			$category->setProducts($productsManager->getResult());
		}

		return $categories;
	}

	public function getAllProducts() {
		$categoryManager = new CategoryManager();

		$categoryManager->query();

		$categories = $categoryManager->getResult();

		foreach ($categories AS $category) {
			$category_id = $category->getCategoryId();

			$productsManager = new ProductsManager();

			$filter['product_category_id'] = $category->getCategoryId();
			//$filter['current_count'] = array('>', 0);
			$filter['archive'] = 'n';

			$productsManager->setFilter($filter);

			$productsManager->query();

			$category->setProducts($productsManager->getResult());
		}

		return $categories;
	}
	
	public function getAllProductsAdmin() {
		$categoryManager = new CategoryManager();
	
		$categoryManager->query();
	
		$categories = $categoryManager->getResult();
	
		foreach ($categories AS $category) {
			$category_id = $category->getCategoryId();
	
			$productsManager = new ProductsManager();
	
			$filter['product_category_id'] = $category->getCategoryId();
			//$filter['current_count'] = array('>', 0);
			//$filter['archive'] = 'n';
	
			$productsManager->setFilter($filter);
	
			$productsManager->query();
	
			$category->setProducts($productsManager->getResult());
		}
	
		return $categories;
	}

	public function getInfo($product_id) {

		$productInfo = new Product_Domain();

		$productInfo->setProductId($product_id);

		$productManager = new ProductManager();

		$productManager->setObj($productInfo);

		$productManager->query();

		return $productManager->getResult();
	}

	public function updateQty($product_id, $product_qty) {
		$productManager = new ProductManager();

		$product = new Product_Domain();

		$product->setProductId($product_id);
		$product->setProductQty($product_qty);

		$productManager->setObj($product);

		$productManager->update();
	}

	public function getProductCategories() {
		$productManager = new ProductManager();

		$productManager->getProductCategory();

		$result = $productManager->getResult();

		return $result;
	}

	public function getInformation($product_id) {

		$productInfo = new Product_Domain();

		$productInfo->setProductId($product_id);

		$productManager = new ProductManager();

		$filter['product_id'] = $product_id;

		$productManager->setFilter($filter);

		$productManager->getProductInfo();

		return $productManager->getResult();
	}

	public function updateProductInfo($product) {
		$productsManager = new ProductsManager();
		$productsManager->setObj($product);
		$productsManager->update();
		
		return $productsManager->getResult();
	}
	
	public function unarchive($product){

		$productsManager = new ProductsManager();
		$productsManager->setObj($product);
		$productsManager->unarchive();
		
		return $productsManager->getResult();
	}
	
	public function addProduct($product){
		$productsManager = new ProductsManager();
		$productsManager->setObj($product);
		$productsManager->add();
		
		return $productsManager->getResult();
	}
	
	public function getAvailableImages(){
		$productsManager = new ProductsManager();
		$productsManager->getAvailableImages();
		return $productsManager->getResult();
	}
}